<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>MySQL 备忘清单
 &#x26;  mysql cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="本备忘单旨在快速理解 MySQL 所涉及的主要概念，提供了最常用的SQL语句，供您参考。

入门，为开发人员分享快速参考备忘单。">
<meta keywords="mysql,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="../style/style.css">
<link rel="stylesheet" href="../style/katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="../index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/mysql.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.5.2"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="mysql-备忘清单"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 230 230">
<path fill="currentColor" d="M176.09375,0 C205.86535,-5.46895416e-15 230,24.1346502 230,53.90625 L230,176.09375 C230,205.86535 205.86535,230 176.09375,230 L53.90625,230 C24.1346502,230 3.64596944e-15,205.86535 0,176.09375 L0,53.90625 C-3.64596944e-15,24.1346502 24.1346502,5.46895416e-15 53.90625,0 L176.09375,0 Z M34.8611719,43.6550781 C31.8918359,50.7590234 39.2626172,57.7533594 41.7647656,61.3623828 C43.6380078,63.8762109 46.0539063,66.7170703 47.3773047,69.5597266 C48.1167188,71.4167969 48.3637891,73.3861719 49.1355469,75.35375 C50.8928906,80.1621875 52.5442188,85.5204688 54.8541016,90.0036719 C56.0867578,92.2991797 57.3814063,94.7069922 58.9213281,96.7823828 C59.8017969,98.0069531 61.3426172,98.5316406 61.6705469,100.49832 C60.1297266,102.686016 60.0237109,105.961719 59.1432422,108.698359 C55.1820313,121.050078 56.7228516,136.352266 62.3308984,145.44625 C64.0936328,148.1775 68.2471094,154.191641 73.8830078,151.894336 C78.8325,149.926758 77.7283203,143.696992 79.1577344,138.230898 C79.4901563,136.920977 79.2691406,136.045898 79.9267969,135.169023 L79.9267969,135.414297 L84.4378516,144.48582 C87.8474219,149.841406 93.7887891,155.417109 98.7355859,159.135742 C101.373398,161.10332 103.464063,164.494023 106.762227,165.694336 L106.762227,165.363711 L106.545703,165.363711 C105.884453,164.383516 104.893477,163.942383 104.013906,163.177813 C102.034648,161.211133 99.8352734,158.805117 98.2944531,156.620117 C93.6773828,150.499961 89.6101563,143.72125 85.9786719,136.726914 C84.2168359,133.338906 82.6760156,129.62207 81.2510938,126.234063 C80.5853516,124.921445 80.5853516,122.951172 79.4883594,122.299805 C77.8361328,124.70043 75.4211328,126.783008 74.2055469,129.732578 C72.1148828,134.431406 71.8983594,140.22543 71.1292969,146.237773 C71.0553255,146.256042 70.9991482,146.26822 70.9559775,146.276964 L70.8606973,146.29554 C70.8058344,146.306853 70.8023405,146.31328 70.7754108,146.356283 L70.7270584,146.428098 C70.715529,146.443877 70.7018652,146.461784 70.6854688,146.482148 C67.1698828,145.610664 65.9569922,141.998945 64.6389844,138.941563 C61.3399219,131.180859 60.7873828,118.72043 63.6471094,109.756719 C64.4161719,107.461211 67.7197266,100.246758 66.4008203,98.0608594 C65.7359766,95.9836719 63.5410938,94.7833594 62.3282031,93.1419141 C60.8987891,91.0647266 59.3633594,88.4430859 58.3723828,86.1475781 C55.7345703,80.0229297 54.4156641,73.2487109 51.5532422,67.1276563 C50.2298438,64.285 47.9226563,61.3327344 46.0512109,58.7119922 C43.9605469,55.7597266 41.6560547,53.6816406 40.0047266,50.1858203 C39.4548828,48.96125 38.6858203,47.0161328 39.5653906,45.7026172 C39.7828125,44.8284375 40.2257422,44.4780469 41.1017188,44.2821875 C42.5302344,43.0567188 46.60375,44.6101172 48.0331641,45.2614844 C52.1003906,46.9029297 55.5099609,48.4320703 58.9195313,50.7248828 C60.4603516,51.8173828 62.1116797,53.8954688 64.0909375,54.4444141 L66.4035156,54.4444141 C69.9235938,55.2080859 73.8803125,54.6887891 77.1757813,55.6689844 C83.0066406,57.5305469 88.2840625,60.26 93.0170313,63.2140625 C107.423477,72.2864844 119.305313,85.1844531 127.331055,100.598945 C128.649961,103.112773 129.199805,105.408281 130.412695,108.031719 C132.725273,113.39 135.585,118.853398 137.889492,124.102969 L138.204251,124.804103 L138.832262,126.208099 C140.82212,130.653803 142.850419,135.071911 145.699609,138.752891 C147.350938,141.048398 153.947266,142.248711 156.915703,143.451719 C159.116875,144.431914 162.52375,145.31418 164.508398,146.514492 C168.245,148.81 171.985195,151.433438 175.502578,153.947266 C177.213676,155.220966 182.475818,157.846431 183.053474,159.996585 L183.077,160.109 L182.374032,160.092842 C173.92852,159.917396 167.324276,160.834138 161.76457,163.17332 C160.117734,163.83457 157.47543,163.83457 157.258008,165.907266 C158.138477,166.776953 158.244492,168.200078 159.01625,169.405781 C160.334258,171.593477 162.646836,174.540352 164.734805,176.069492 L171.773164,181.098945 C176.062305,183.719688 180.901289,185.251523 185.079922,187.874961 C187.500313,189.405898 189.918906,191.373477 192.339297,193.014922 C193.571953,193.889102 194.318555,195.313125 195.859375,195.85668 L195.859375,195.526055 C195.090313,194.545859 194.868398,193.12543 194.102031,192.027539 L190.798477,188.859648 C187.611719,184.597461 183.649609,180.87793 179.360469,177.820547 C175.840391,175.41543 168.144375,172.137031 166.714063,168.094961 L166.497539,167.849688 C168.913438,167.605313 171.773164,166.754492 174.084844,166.098633 C177.826836,165.118438 181.233711,165.334063 185.079922,164.351172 L190.362734,162.820234 L190.362734,161.840039 C188.382578,159.873359 186.953164,157.249023 184.865195,155.392852 C179.257148,150.583516 173.093867,145.881992 166.714063,141.947734 C163.305391,139.762734 158.904844,138.338711 155.276055,136.484336 C154.858139,136.276939 154.353138,136.102333 153.833509,135.926087 L153.311688,135.748018 C152.355916,135.41588 151.432161,135.039648 150.986914,134.407148 C149.002266,132.006523 147.905273,128.833242 146.475859,125.989688 L137.458242,106.971563 C135.478984,102.708477 134.266094,98.4453906 131.850195,94.5111328 C120.52,75.92875 108.199727,64.6722266 89.2822266,53.6304297 C85.215,51.3349219 80.3760156,50.3475391 75.2306641,49.1472266 L66.9830078,48.7069922 C65.2202734,47.9424219 63.4620313,45.8643359 61.9221094,44.8796484 C55.6528125,40.9444922 39.4845313,32.4201172 34.8611719,43.6550781 Z M70.9388281,65.1250391 C69.4196472,65.1116878 67.9051429,65.2953007 66.4331641,65.6712891 L66.4331641,65.9165625 L66.6496875,65.9165625 C67.5301563,67.6658203 69.0709766,68.8688281 70.1697656,70.3997656 L72.7015625,75.6466406 L72.9189844,75.4022656 C74.4589063,74.3097656 75.2306641,72.5605078 75.2306641,69.9397656 C74.5703125,69.1751953 74.4616016,68.4079297 73.9117578,67.6433594 C73.2514063,66.5508594 71.8219922,66.0019141 70.9388281,65.1304297 Z"></path></svg><a aria-hidden="true" tabindex="-1" href="#mysql-备忘清单"><span class="icon icon-link"></span></a>MySQL 备忘清单</h1><div class="wrap-body">
<p>本备忘单旨在快速理解 <a href="https://mysql.com">MySQL</a> 所涉及的主要概念，提供了最常用的SQL语句，供您参考。</p>
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#介绍">介绍</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#登录mysql">登录MySQL</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#常用的">常用的</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#数据库-database">数据库 Database</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#表-table">表 Table</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#proccess">Proccess</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#查看-mysql-信息">查看 MySQL 信息</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#退出mysql会话">退出MySQL会话</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#备份">备份</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#mysql-示例">MySQL 示例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#管理表格">管理表格</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#从表中查询数据">从表中查询数据</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#从多个表查询">从多个表查询</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用-sql-约束">使用 SQL 约束</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#修改数据">修改数据</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#管理视图">管理视图</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#管理触发器">管理触发器</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#when">WHEN</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#event">EVENT</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#trigger_type">TRIGGER_TYPE</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#管理索引">管理索引</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#mysql-数据类型">MySQL 数据类型</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#strings">Strings</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#date--time">Date &#x26; time</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#numeric">Numeric</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="介绍"><a aria-hidden="true" tabindex="-1" href="#介绍"><span class="icon icon-link"></span></a>介绍</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<p>MySQL 为关系型数据库(Relational Database Management System)，一个关系型数据库由一个或数个表格组成，如下所示的一个表格</p>
<hr>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">    name ▼ 键            ▼ 列<span class="token punctuation">(</span>col<span class="token punctuation">)</span>
</span><span class="code-line">┌┈┈┈┈┬┈┈┈┈┈┈┈┈┬┈┈┈┈┈┈┬┈┈┈┈┈┈┈┐
</span><span class="code-line">┆ <span class="token function">id</span> ┆ name   ┆ uid  ┆ level ┆  ◀ 表头header
</span><span class="code-line">├┈┈┈┈┼┈┈┈┈┈┈┈┈┤┈┈┈┈┈┈┤┈┈┈┈┈┈┈┤
</span><span class="code-line">┆  <span class="token number">1</span> ┆ mysql  ┆ <span class="token number">0</span>    ┆ <span class="token number">3</span>     ┆
</span><span class="code-line">├┈┈┈┈┼┈┈┈┈┈┈┈┈┤┈┈┈┈┈┈┤┈┈┈┈┈┈┈┤
</span><span class="code-line">┆  <span class="token number">2</span> ┆ redis  ┆ <span class="token number">12</span>   ┆ <span class="token number">1</span>     ┆  ◀ 行 row
</span><span class="code-line">└┈┈┈┈┴┈┈┈┈┈┈┈┈┴┈┈┈┈┈┈┴┈┈┈┈┈┈┈┘
</span><span class="code-line">    redis ▲ 值
</span></code></pre>
<hr>
<ul>
<li><code>表头(header)</code> 每一列的名称</li>
<li><code>列(col)</code> 具有相同数据类型的数据的集合</li>
<li><code>行(row)</code> 每一行用来描述某个人/物的具体信息</li>
<li><code>值(value)</code> 行的具体信息，每个值与该列数据类型相同</li>
<li><code>键(key)</code> 用来识别某个特定的人/物的方法，有唯一性</li>
</ul>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="登录mysql"><a aria-hidden="true" tabindex="-1" href="#登录mysql"><span class="icon icon-link"></span></a>登录MySQL</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 默认用户名&#x3C;root>，-p 是密码，</span>
</span><span class="code-line"><span class="token comment"># ⚠️参数后面不需要空格</span>
</span><span class="code-line">mysql <span class="token parameter variable">-h</span> <span class="token number">127.0</span>.0.1 <span class="token parameter variable">-u</span> <span class="token operator">&#x3C;</span>用户名<span class="token operator">></span> -p<span class="token operator">&#x3C;</span>密码<span class="token operator">></span>
</span><span class="code-line">mysql <span class="token parameter variable">-D</span> 数据库名 <span class="token parameter variable">-h</span> 主机名 <span class="token parameter variable">-u</span> 用户名 <span class="token parameter variable">-p</span>
</span><span class="code-line">mysql <span class="token parameter variable">-h</span> <span class="token operator">&#x3C;</span>host<span class="token operator">></span> <span class="token parameter variable">-P</span> <span class="token operator">&#x3C;</span>端口号<span class="token operator">></span> <span class="token parameter variable">-u</span> <span class="token operator">&#x3C;</span>user<span class="token operator">></span> <span class="token parameter variable">-p</span> <span class="token punctuation">[</span>db_name<span class="token punctuation">]</span>
</span><span class="code-line">mysql <span class="token parameter variable">-h</span> <span class="token operator">&#x3C;</span>host<span class="token operator">></span> <span class="token parameter variable">-u</span> <span class="token operator">&#x3C;</span>user<span class="token operator">></span> <span class="token parameter variable">-p</span> <span class="token punctuation">[</span>db_name<span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="常用的"><a aria-hidden="true" tabindex="-1" href="#常用的"><span class="icon icon-link"></span></a>常用的</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<h4 id="数据库-database"><a aria-hidden="true" tabindex="-1" href="#数据库-database"><span class="icon icon-link"></span></a>数据库 Database</h4>





























<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>CREATE DATABASE</code> db <code>;</code></td><td align="left"><code>创建</code>数据库</td></tr><tr><td align="left"><code>SHOW DATABASES;</code></td><td align="left"><code>列出</code>数据库</td></tr><tr><td align="left"><code>USE</code> db<code>;</code></td><td align="left"><code>切换</code>到数据库</td></tr><tr><td align="left"><code>CONNECT</code> db <code>;</code></td><td align="left"><code>切换</code>到数据库</td></tr><tr><td align="left"><code>DROP DATABASE</code> db<code>;</code></td><td align="left"><code>删除</code>数据库</td></tr></tbody></table>
<h4 id="表-table"><a aria-hidden="true" tabindex="-1" href="#表-table"><span class="icon icon-link"></span></a>表 Table</h4>

































<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>SHOW TABLES;</code></td><td align="left">列出当前数据库的表</td></tr><tr><td align="left"><code>SHOW FIELDS FROM</code> t<code>;</code></td><td align="left">表的列表字段</td></tr><tr><td align="left"><code>DESC</code> t<code>;</code></td><td align="left">显示表格结构</td></tr><tr><td align="left"><code>SHOW CREATE TABLE</code>t<code>;</code></td><td align="left">显示创建表sql</td></tr><tr><td align="left"><code>TRUNCATE TABLE</code>t<code>;</code></td><td align="left">删除表中的所有数据</td></tr><tr><td align="left"><code>DROP TABLE</code>t<code>;</code></td><td align="left">删除表格</td></tr></tbody></table>
<h4 id="proccess"><a aria-hidden="true" tabindex="-1" href="#proccess"><span class="icon icon-link"></span></a>Proccess</h4>

















<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>show processlist;</code></td><td align="left">列出进程</td></tr><tr><td align="left"><code>kill</code> pid<code>;</code></td><td align="left">杀死进程</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="查看-mysql-信息"><a aria-hidden="true" tabindex="-1" href="#查看-mysql-信息"><span class="icon icon-link"></span></a>查看 MySQL 信息</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 显示当前mysql的version的各种信息</span>
</span><span class="code-line">mysql<span class="token operator">></span> status<span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment"># 显示当前mysql的version信息</span>
</span><span class="code-line">mysql<span class="token operator">></span> <span class="token keyword">select</span> version<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
</span><span class="code-line"><span class="token comment"># 查看 MySQL 端口号</span>
</span><span class="code-line">mysql<span class="token operator">></span> show global variables like <span class="token string">'port'</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="退出mysql会话"><a aria-hidden="true" tabindex="-1" href="#退出mysql会话"><span class="icon icon-link"></span></a>退出MySQL会话</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">mysql<span class="token operator">></span> <span class="token builtin class-name">exit</span> 
</span></code></pre>
<p>退出 <code>quit;</code> 或 <code>\q;</code> 一样的效果</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="备份"><a aria-hidden="true" tabindex="-1" href="#备份"><span class="icon icon-link"></span></a>备份</h3><div class="wrap-body">
<p>创建备份</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line">mysqldump <span class="token operator">-</span>u <span class="token keyword">user</span> <span class="token operator">-</span>p db_name <span class="token operator">></span> db<span class="token punctuation">.</span><span class="token keyword">sql</span>
</span></code></pre>
<p>导出不带架构的数据库</p>
<pre class="wrap-text "><code class="language-shell code-highlight"><span class="code-line">mysqldump <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> db_name --no-data<span class="token operator">=</span>true --add-drop-table<span class="token operator">=</span>false <span class="token operator">></span> db.sql
</span></code></pre>
<!--rehype:className=wrap-text -->
<p>恢复备份</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">mysql <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> db_name <span class="token operator">&#x3C;</span> db.sql
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="mysql-示例"><a aria-hidden="true" tabindex="-1" href="#mysql-示例"><span class="icon icon-link"></span></a>MySQL 示例</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="管理表格"><a aria-hidden="true" tabindex="-1" href="#管理表格"><span class="icon icon-link"></span></a>管理表格</h3><div class="wrap-body">
<p>创建一个包含三列的新表</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> t <span class="token punctuation">(</span>
</span><span class="code-line">    id    <span class="token keyword">INT</span><span class="token punctuation">,</span>
</span><span class="code-line">    name  <span class="token keyword">VARCHAR</span> <span class="token keyword">DEFAULT</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
</span><span class="code-line">    price <span class="token keyword">INT</span> <span class="token keyword">DEFAULT</span> <span class="token number">0</span>
</span><span class="code-line">    <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>从数据库中删除表</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">DROP</span> <span class="token keyword">TABLE</span> t <span class="token punctuation">;</span>
</span></code></pre>
<p>向表中添加新列</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> t <span class="token keyword">ADD</span> <span class="token keyword">column</span><span class="token punctuation">;</span>
</span></code></pre>
<p>从表中删除列c</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> t <span class="token keyword">DROP</span> <span class="token keyword">COLUMN</span> c <span class="token punctuation">;</span>
</span></code></pre>
<p>添加约束</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> t <span class="token keyword">ADD</span> <span class="token keyword">constraint</span><span class="token punctuation">;</span>
</span></code></pre>
<p>删除约束</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> t <span class="token keyword">DROP</span> <span class="token keyword">constraint</span><span class="token punctuation">;</span>
</span></code></pre>
<p>将表从t1重命名为t2</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> t1 <span class="token keyword">RENAME</span> <span class="token keyword">TO</span> t2<span class="token punctuation">;</span>
</span></code></pre>
<p>将列c1重命名为c2</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> t1 <span class="token keyword">RENAME</span> c1 <span class="token keyword">TO</span> c2 <span class="token punctuation">;</span>
</span></code></pre>
<p>将列c1的数据类型改为datatype</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> t1 <span class="token keyword">MODIFY</span> c1 datatype<span class="token punctuation">;</span>
</span></code></pre>
<p>删除表中的所有数据</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">TRUNCATE</span> <span class="token keyword">TABLE</span> t<span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="从表中查询数据"><a aria-hidden="true" tabindex="-1" href="#从表中查询数据"><span class="icon icon-link"></span></a>从表中查询数据</h3><div class="wrap-body">
<p>从表中查询列c1、c2中的数据</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 <span class="token keyword">FROM</span> t
</span></code></pre>
<p>查询表中的所有行和列</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> t
</span></code></pre>
<p>查询数据并使用条件筛选行</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 <span class="token keyword">FROM</span> t
</span><span class="code-line"><span class="token keyword">WHERE</span> condition
</span></code></pre>
<p>查询表中的不同行</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> <span class="token keyword">DISTINCT</span> c1 <span class="token keyword">FROM</span> t
</span><span class="code-line"><span class="token keyword">WHERE</span> condition
</span></code></pre>
<p>按升序或降序对结果集排序</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 <span class="token keyword">FROM</span> t
</span><span class="code-line"><span class="token keyword">ORDER</span> <span class="token keyword">BY</span> c1 <span class="token keyword">ASC</span> <span class="token punctuation">[</span><span class="token keyword">DESC</span><span class="token punctuation">]</span>
</span></code></pre>
<p>跳过行的偏移并返回下n行</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 <span class="token keyword">FROM</span> t
</span><span class="code-line"><span class="token keyword">ORDER</span> <span class="token keyword">BY</span> c1 
</span><span class="code-line"><span class="token keyword">LIMIT</span> n <span class="token keyword">OFFSET</span> <span class="token keyword">offset</span>
</span></code></pre>
<p>使用聚合函数对行进行分组</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> aggregate<span class="token punctuation">(</span>c2<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">FROM</span> t
</span><span class="code-line"><span class="token keyword">GROUP</span> <span class="token keyword">BY</span> c1
</span></code></pre>
<p>使用HAVING子句筛选组</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> aggregate<span class="token punctuation">(</span>c2<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">FROM</span> t
</span><span class="code-line"><span class="token keyword">GROUP</span> <span class="token keyword">BY</span> c1
</span><span class="code-line"><span class="token keyword">HAVING</span> condition
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="从多个表查询"><a aria-hidden="true" tabindex="-1" href="#从多个表查询"><span class="icon icon-link"></span></a>从多个表查询</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>内部连接 t1 和 t2</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 
</span><span class="code-line"><span class="token keyword">FROM</span> t1
</span><span class="code-line"><span class="token keyword">INNER</span> <span class="token keyword">JOIN</span> t2 <span class="token keyword">ON</span> condition
</span></code></pre>
<p>左连接t1和t1</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 
</span><span class="code-line"><span class="token keyword">FROM</span> t1
</span><span class="code-line"><span class="token keyword">LEFT</span> <span class="token keyword">JOIN</span> t2 <span class="token keyword">ON</span> condition
</span></code></pre>
<p>右连接t1和t2</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 
</span><span class="code-line"><span class="token keyword">FROM</span> t1
</span><span class="code-line"><span class="token keyword">RIGHT</span> <span class="token keyword">JOIN</span> t2 <span class="token keyword">ON</span> condition
</span></code></pre>
<p>执行完全外部连接</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 
</span><span class="code-line"><span class="token keyword">FROM</span> t1
</span><span class="code-line"><span class="token keyword">FULL</span> <span class="token keyword">OUTER</span> <span class="token keyword">JOIN</span> t2 <span class="token keyword">ON</span> condition
</span></code></pre>
<p>生成表中行的笛卡尔积</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 
</span><span class="code-line"><span class="token keyword">FROM</span> t1
</span><span class="code-line"><span class="token keyword">CROSS</span> <span class="token keyword">JOIN</span> t2
</span></code></pre>
<p>执行交叉连接的另一种方法</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 
</span><span class="code-line"><span class="token keyword">FROM</span> t1<span class="token punctuation">,</span> t2
</span></code></pre>
<p>使用INNER Join子句将t1连接到自身</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2
</span><span class="code-line"><span class="token keyword">FROM</span> t1 A
</span><span class="code-line"><span class="token keyword">INNER</span> <span class="token keyword">JOIN</span> t1 B <span class="token keyword">ON</span> condition
</span></code></pre>
<p>使用SQL运算符，合并两个查询中的行</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 <span class="token keyword">FROM</span> t1
</span><span class="code-line"><span class="token keyword">UNION</span> <span class="token punctuation">[</span><span class="token keyword">ALL</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 <span class="token keyword">FROM</span> t2
</span></code></pre>
<p>返回两个查询的交集</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 <span class="token keyword">FROM</span> t1
</span><span class="code-line"><span class="token keyword">INTERSECT</span>
</span><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 <span class="token keyword">FROM</span> t2
</span></code></pre>
<p>从另一个结果集中减去一个结果集</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 <span class="token keyword">FROM</span> t1
</span><span class="code-line">MINUS
</span><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 <span class="token keyword">FROM</span> t2
</span></code></pre>
<p>使用模式匹配%查询行_</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 <span class="token keyword">FROM</span> t1
</span><span class="code-line"><span class="token keyword">WHERE</span> c1 <span class="token punctuation">[</span><span class="token operator">NOT</span><span class="token punctuation">]</span> <span class="token operator">LIKE</span> pattern
</span></code></pre>
<p>查询列表中的行</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 <span class="token keyword">FROM</span> t
</span><span class="code-line"><span class="token keyword">WHERE</span> c1 <span class="token punctuation">[</span><span class="token operator">NOT</span><span class="token punctuation">]</span> <span class="token operator">IN</span> value_list
</span></code></pre>
<p>查询两个值之间的行</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 <span class="token keyword">FROM</span> t
</span><span class="code-line"><span class="token keyword">WHERE</span>  c1 <span class="token operator">BETWEEN</span> low <span class="token operator">AND</span> high
</span></code></pre>
<p>检查表中的值是否为NULL</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2 <span class="token keyword">FROM</span> t
</span><span class="code-line"><span class="token keyword">WHERE</span>  c1 <span class="token operator">IS</span> <span class="token punctuation">[</span><span class="token operator">NOT</span><span class="token punctuation">]</span> <span class="token boolean">NULL</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="使用-sql-约束"><a aria-hidden="true" tabindex="-1" href="#使用-sql-约束"><span class="icon icon-link"></span></a>使用 SQL 约束</h3><div class="wrap-body">
<p>将c1和c2设置为主键</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> t<span class="token punctuation">(</span>
</span><span class="code-line">    c1 <span class="token keyword">INT</span><span class="token punctuation">,</span> c2 <span class="token keyword">INT</span><span class="token punctuation">,</span> c3 <span class="token keyword">VARCHAR</span><span class="token punctuation">,</span>
</span><span class="code-line">    <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>c1<span class="token punctuation">,</span>c2<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>将c2列设置为外键</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> t1<span class="token punctuation">(</span>
</span><span class="code-line">    c1 <span class="token keyword">INT</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span><span class="token punctuation">,</span>  
</span><span class="code-line">    c2 <span class="token keyword">INT</span><span class="token punctuation">,</span>
</span><span class="code-line">    <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>c2<span class="token punctuation">)</span> <span class="token keyword">REFERENCES</span> t2<span class="token punctuation">(</span>c2<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>使c1和c2中的值唯一</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> t<span class="token punctuation">(</span>
</span><span class="code-line">    c1 <span class="token keyword">INT</span><span class="token punctuation">,</span> c1 <span class="token keyword">INT</span><span class="token punctuation">,</span>
</span><span class="code-line">    <span class="token keyword">UNIQUE</span><span class="token punctuation">(</span>c2<span class="token punctuation">,</span>c3<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>确保c1>0和c1>=c2中的值</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> t<span class="token punctuation">(</span>
</span><span class="code-line">  c1 <span class="token keyword">INT</span><span class="token punctuation">,</span> c2 <span class="token keyword">INT</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token keyword">CHECK</span><span class="token punctuation">(</span>c1<span class="token operator">></span> <span class="token number">0</span> <span class="token operator">AND</span> c1 <span class="token operator">>=</span> c2<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>c2列中的设置值不为NULL</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> t<span class="token punctuation">(</span>
</span><span class="code-line">     c1 <span class="token keyword">INT</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span><span class="token punctuation">,</span>
</span><span class="code-line">     c2 <span class="token keyword">VARCHAR</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span>
</span><span class="code-line"><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="修改数据"><a aria-hidden="true" tabindex="-1" href="#修改数据"><span class="icon icon-link"></span></a>修改数据</h3><div class="wrap-body">
<p>在表格中插入一行</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> t<span class="token punctuation">(</span>column_list<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">VALUES</span><span class="token punctuation">(</span>value_list<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>在表格中插入多行</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> t<span class="token punctuation">(</span>column_list<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">VALUES</span> <span class="token punctuation">(</span>value_list<span class="token punctuation">)</span><span class="token punctuation">,</span> 
</span><span class="code-line">       <span class="token punctuation">(</span>value_list<span class="token punctuation">)</span><span class="token punctuation">,</span> …<span class="token punctuation">;</span>
</span></code></pre>
<p>将行从t2插入t1</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> t1<span class="token punctuation">(</span>column_list<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">SELECT</span> column_list
</span><span class="code-line"><span class="token keyword">FROM</span> t2<span class="token punctuation">;</span>
</span></code></pre>
<p>更新列c1中所有行的新值</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">UPDATE</span> t
</span><span class="code-line"><span class="token keyword">SET</span> c1 <span class="token operator">=</span> new_value<span class="token punctuation">;</span>
</span></code></pre>
<p>更新列c1、c2中与条件匹配的值</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">UPDATE</span> t
</span><span class="code-line"><span class="token keyword">SET</span> c1 <span class="token operator">=</span> new_value<span class="token punctuation">,</span> 
</span><span class="code-line">        c2 <span class="token operator">=</span> new_value
</span><span class="code-line"><span class="token keyword">WHERE</span> condition<span class="token punctuation">;</span>
</span></code></pre>
<p>删除表中的所有数据</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">DELETE</span> <span class="token keyword">FROM</span> t<span class="token punctuation">;</span>
</span></code></pre>
<p>删除表中的行子集</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">DELETE</span> <span class="token keyword">FROM</span> t
</span><span class="code-line"><span class="token keyword">WHERE</span> condition<span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="管理视图"><a aria-hidden="true" tabindex="-1" href="#管理视图"><span class="icon icon-link"></span></a>管理视图</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>创建由c1和c2组成的新视图</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">CREATE</span> <span class="token keyword">VIEW</span> v<span class="token punctuation">(</span>c1<span class="token punctuation">,</span>c2<span class="token punctuation">)</span> 
</span><span class="code-line"><span class="token keyword">AS</span>
</span><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2
</span><span class="code-line"><span class="token keyword">FROM</span> t<span class="token punctuation">;</span>
</span></code></pre>
<p>使用选中选项创建新视图</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">CREATE</span> <span class="token keyword">VIEW</span> v<span class="token punctuation">(</span>c1<span class="token punctuation">,</span>c2<span class="token punctuation">)</span> 
</span><span class="code-line"><span class="token keyword">AS</span>
</span><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2
</span><span class="code-line"><span class="token keyword">FROM</span> t<span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">WITH</span> <span class="token punctuation">[</span><span class="token keyword">CASCADED</span> <span class="token operator">|</span> <span class="token keyword">LOCAL</span><span class="token punctuation">]</span> <span class="token keyword">CHECK</span> <span class="token keyword">OPTION</span><span class="token punctuation">;</span>
</span></code></pre>
<p>创建递归视图</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">CREATE</span> RECURSIVE <span class="token keyword">VIEW</span> v 
</span><span class="code-line"><span class="token keyword">AS</span>
</span><span class="code-line"><span class="token keyword">select</span><span class="token operator">-</span>statement <span class="token comment">-- anchor part</span>
</span><span class="code-line"><span class="token keyword">UNION</span> <span class="token punctuation">[</span><span class="token keyword">ALL</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token keyword">select</span><span class="token operator">-</span>statement<span class="token punctuation">;</span> <span class="token comment">-- recursive part</span>
</span></code></pre>
<p>创建临时视图</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">CREATE</span> <span class="token keyword">TEMPORARY</span> <span class="token keyword">VIEW</span> v 
</span><span class="code-line"><span class="token keyword">AS</span>
</span><span class="code-line"><span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span> c2
</span><span class="code-line"><span class="token keyword">FROM</span> t<span class="token punctuation">;</span>
</span></code></pre>
<p>删除视图</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">DROP</span> <span class="token keyword">VIEW</span> view_name<span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="管理触发器"><a aria-hidden="true" tabindex="-1" href="#管理触发器"><span class="icon icon-link"></span></a>管理触发器</h3><div class="wrap-body">
<p>创建或修改触发器</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">CREATE</span> <span class="token operator">OR</span> <span class="token keyword">MODIFY</span> <span class="token keyword">TRIGGER</span> trigger_name
</span><span class="code-line"><span class="token keyword">WHEN</span> EVENT
</span><span class="code-line"><span class="token keyword">ON</span> table_name TRIGGER_TYPE
</span><span class="code-line"><span class="token keyword">EXECUTE</span> stored_procedure<span class="token punctuation">;</span>
</span></code></pre>
<h4 id="when"><a aria-hidden="true" tabindex="-1" href="#when"><span class="icon icon-link"></span></a>WHEN</h4>

















<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>BEFORE</code></td><td align="left">在事件发生前调用</td></tr><tr><td align="left"><code>AFTER</code></td><td align="left">事件发生后调用</td></tr></tbody></table>
<h4 id="event"><a aria-hidden="true" tabindex="-1" href="#event"><span class="icon icon-link"></span></a>EVENT</h4>





















<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>INSERT</code></td><td align="left">为INSERT调用</td></tr><tr><td align="left"><code>UPDATE</code></td><td align="left">调用UPDATE</td></tr><tr><td align="left"><code>DELETE</code></td><td align="left">调用DELETE</td></tr></tbody></table>
<h4 id="trigger_type"><a aria-hidden="true" tabindex="-1" href="#trigger_type"><span class="icon icon-link"></span></a>TRIGGER_TYPE</h4>

















<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>FOR EACH ROW</code></td><td align="left">-</td></tr><tr><td align="left"><code>FOR EACH STATEMENT</code></td><td align="left">-</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="管理索引"><a aria-hidden="true" tabindex="-1" href="#管理索引"><span class="icon icon-link"></span></a>管理索引</h3><div class="wrap-body">
<p>在t表的c1和c2上创建索引</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">CREATE</span> <span class="token keyword">INDEX</span> idx_name 
</span><span class="code-line"><span class="token keyword">ON</span> t<span class="token punctuation">(</span>c1<span class="token punctuation">,</span>c2<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>在t表的c3、c4上创建唯一索引</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">CREATE</span> <span class="token keyword">UNIQUE</span> <span class="token keyword">INDEX</span> idx_name
</span><span class="code-line"><span class="token keyword">ON</span> t<span class="token punctuation">(</span>c3<span class="token punctuation">,</span>c4<span class="token punctuation">)</span>
</span></code></pre>
<p>删除索引</p>
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line"><span class="token keyword">DROP</span> <span class="token keyword">INDEX</span> idx_name<span class="token punctuation">;</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="mysql-数据类型"><a aria-hidden="true" tabindex="-1" href="#mysql-数据类型"><span class="icon icon-link"></span></a>MySQL 数据类型</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="strings"><a aria-hidden="true" tabindex="-1" href="#strings"><span class="icon icon-link"></span></a>Strings</h3><div class="wrap-body">





















































<table><thead><tr><th>-</th><th>-</th></tr></thead><tbody><tr><td><code>CHAR</code></td><td>String (0 - 255)</td></tr><tr><td><code>VARCHAR</code></td><td>String (0 - 255)</td></tr><tr><td><code>TINYTEXT</code></td><td>String (0 - 255)</td></tr><tr><td><code>TEXT</code></td><td>String (0 - 65535)</td></tr><tr><td><code>BLOB</code></td><td>String (0 - 65535)</td></tr><tr><td><code>MEDIUMTEXT</code></td><td>String (0 - 16777215)</td></tr><tr><td><code>MEDIUMBLOB</code></td><td>String (0 - 16777215)</td></tr><tr><td><code>LONGTEXT</code></td><td>String (0 - 429496­7295)</td></tr><tr><td><code>LONGBLOB</code></td><td>String (0 - 429496­7295)</td></tr><tr><td><code>ENUM</code></td><td>One of preset options</td></tr><tr><td><code>SET</code></td><td>Selection of preset options</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="date--time"><a aria-hidden="true" tabindex="-1" href="#date--time"><span class="icon icon-link"></span></a>Date &#x26; time</h3><div class="wrap-body">





























<table><thead><tr><th>Data Type</th><th>Format</th></tr></thead><tbody><tr><td><code>DATE</code></td><td>yyyy-MM-dd</td></tr><tr><td><code>TIME</code></td><td>hh:mm:ss</td></tr><tr><td><code>DATETIME</code></td><td>yyyy-MM-dd hh:mm:ss</td></tr><tr><td><code>TIMESTAMP</code></td><td>yyyy-MM-dd hh:mm:ss</td></tr><tr><td><code>YEAR</code></td><td>yyyy</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="numeric"><a aria-hidden="true" tabindex="-1" href="#numeric"><span class="icon icon-link"></span></a>Numeric</h3><div class="wrap-body">









































<table><thead><tr><th>-</th><th>-</th></tr></thead><tbody><tr><td><code>TINYINT x</code></td><td>Integer (-128 to 127)</td></tr><tr><td><code>SMALLINT x</code></td><td>Integer (-32768 to 32767)</td></tr><tr><td><code>MEDIUMINT x</code></td><td>Integer (-8388608 to 8388607)</td></tr><tr><td><code>INT x</code></td><td>Integer (-2147­483648 to 214748­3647)</td></tr><tr><td><code>BIGINT x</code></td><td>Integer (-9223­372­036­854­775808 to 922337­203­685­477­5807)</td></tr><tr><td><code>FLOAT</code></td><td>Decimal (precise to 23 digits)</td></tr><tr><td><code>DOUBLE</code></td><td>Decimal (24 to 53 digits)</td></tr><tr><td><code>DECIMAL</code></td><td>"­DOU­BLE­" stored as string</td></tr></tbody></table>
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="另见"><a aria-hidden="true" tabindex="-1" href="#另见"><span class="icon icon-link"></span></a>另见</h2><div class="wrap-body">
<ul>
<li><a href="http://www.w3school.com.cn/sql/index.asp">SQL 基础教程</a> <em>(w3school.com.cn)</em></li>
<li><a href="http://www.1keydata.com/cn/sql/sql-count.php">SQL 语句教程</a> <em>(1keydata.com)</em></li>
<li><a href="https://jaywcjlove.github.io/mysql-tutorial/21-minutes-MySQL-basic-entry.html">21分钟 MySQL 基础入门</a> <em>(jaywcjlove.github.io)</em></li>
</ul>
</div></div><div class="h2wrap-body"></div></div></div><script src="https://giscus.app/client.js" data-repo="jaywcjlove/reference" data-repo-id="R_kgDOID2-Mw" data-category="Q&#x26;A" data-category-id="DIC_kwDOID2-M84CS5wo" data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="bottom" data-theme="dark" data-lang="zh-CN" crossorigin="anonymous" async></script><div class="giscus"></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 Kenny Wang.</footer></footer><script src="../data.js?v=1.5.2" defer></script><script src="../js/fuse.min.js?v=1.5.2" defer></script><script src="../js/main.js?v=1.5.2" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
